package com.zego.wawaji.ui.dialog.pay;

import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.log.PToast;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.ui.base.recycler.BaseRecyclerViewHolder;
import com.zego.wawaji.ui.bean.pay.Pay;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.ui.dialog.popup.BottomPopup;
import com.zego.wawaji.wawaji_client.App;

import java.util.List;

/**
 * 支付弹框
 * Created by Su on 2017/12/26.
 */
public class PayDialog extends BottomPopup implements RequestComplete, View.OnClickListener, BaseRecyclerViewHolder.OnItemClickListener {
    private RecyclerView recyclerView;  // 商品列表
    private PayDialogAdapter adapter;

    public PayDialog(FragmentActivity activity) {
        super(activity);
    }

    @Override
    protected View makeContentView() {
        setSize(screenWidthPixels, (int) (screenHeightPixels * 0.9));
        View inflate = LayoutInflater.from(activity).inflate(R.layout.pay_dialog, null);
        inflate.findViewById(R.id.pay_close).setOnClickListener(this);
        recyclerView = (RecyclerView) inflate.findViewById(R.id.pay_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new PayDialogAdapter();
        adapter.setList(ModuleMgr.getWawaMgr().getPayList().getPayCommonList());
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);

        initLimitView(inflate);
        return inflate;
    }

    private void initLimitView(View inflate) {
        final List<Pay> limitList = ModuleMgr.getWawaMgr().getPayList().getPayNewList();

        // 限购1
        RelativeLayout pay_item01 = (RelativeLayout) inflate.findViewById(R.id.pay_item01);
        TextView pr_coin01 = (TextView) inflate.findViewById(R.id.pr_coin01);
        TextView pr_desc01 = (TextView) inflate.findViewById(R.id.pr_desc01);
        TextView pr_money01 = (TextView) inflate.findViewById(R.id.pr_money01);

        // 限购2
        RelativeLayout pay_item02 = (RelativeLayout) inflate.findViewById(R.id.pay_item02);
        TextView pr_coin02 = (TextView) inflate.findViewById(R.id.pr_coin02);
        TextView pr_desc02 = (TextView) inflate.findViewById(R.id.pr_desc02);
        TextView pr_money02 = (TextView) inflate.findViewById(R.id.pr_money02);

        pay_item01.setVisibility(View.GONE);
        pay_item02.setVisibility(View.GONE);
        if (limitList.size() <= 0) return;
        for (int i = 0; i < 2; i++) {
            final int temp = i;
            if (i == 0) {
                pay_item01.setVisibility(View.VISIBLE);
                pr_coin01.setText(String.valueOf(limitList.get(i).getWwCoinCz() + limitList.get(i).getWwCoinZs()));
                pr_money01.setText(limitList.get(i).getIntPrice() + Html.fromHtml("&#165;").toString());
                pr_desc01.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(limitList.get(i).getRemark())) {
                    pr_desc01.setVisibility(View.VISIBLE);
                    pr_desc01.setText(limitList.get(i).getRemark());
                }
                pay_item01.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!limitList.get(temp).enableBuy()) {
                            PToast.showShort("该商品仅能购买一次");
                        } else {
                            PickerDialogUtil.showPayBottomDialog(App.getActivity(), limitList.get(temp));
                        }
                    }
                });
            }

            if (i == 1) {
                if (limitList.size() <= 1) return;
                pay_item02.setVisibility(View.VISIBLE);
                pr_coin02.setText(String.valueOf(limitList.get(i).getWwCoinCz() + limitList.get(i).getWwCoinZs()));
                pr_money02.setText(limitList.get(i).getIntPrice() + Html.fromHtml("&#165;").toString());
                pr_desc02.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(limitList.get(i).getRemark())) {
                    pr_desc02.setVisibility(View.VISIBLE);
                    pr_desc02.setText(limitList.get(i).getRemark());
                }
                pay_item02.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!limitList.get(temp).enableBuy()) {
                            PToast.showShort("该商品仅能购买一次");
                        } else {
                            PickerDialogUtil.showPayBottomDialog(App.getActivity(), limitList.get(temp));
                        }
                    }
                });

            }
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
    }

    @Override
    public void onItemClick(View convertView, int position) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay_close:
                dismiss();
                break;
        }
    }
}
